package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.config.template.GreenTemplate;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.pojo.Log;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class LogListener {
    @Resource
    private LogMapper logMapper;
    @DubboReference
    private MovementApi movementApi;
    @Autowired
    private GreenTemplate greenTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "tanhua.log.user",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "log.*"
    ))
    public void listenerUser(String message){
        JSONObject jsonObject = JSON.parseObject(message);
        Log log = new Log();
        log.setUserId(jsonObject.getLong("userId"));
        log.setType(jsonObject.getString("type"));
        log.setLogTime(jsonObject.getString("logTime"));

        logMapper.insert(log);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "tanhua.green.queue",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "tanhua.green.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "green.movement"
    ))
    public void listenerMovement(String movementId){
        Movement movement = movementApi.findById(movementId);

        try {
            if(movement!=null&&movement.getState()==0){
                Map<String, String> textScan = greenTemplate.greenTextScan(movement.getTextContent());
                Map<String, String> imageScan = greenTemplate.imageScan(movement.getMedias());
                String textSuggestion = textScan.get("suggestion");
                String imageSuggestion = imageScan.get("suggestion");
                if("pass".equals(textSuggestion)&&"pass".equals(imageSuggestion)){
                    movementApi.updateState(movementId,1);
                }else if("block".equals(textSuggestion)||"block".equals(imageSuggestion)){
                    movementApi.updateState(movementId, 2);
                }else {
                    movementApi.updateState(movementId, 3);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
